<?php


namespace app\common\dao\activity;

use app\common\model\activity\NewActivity;
use app\common\model\activity\NewActivityProduct;
use app\common\repositories\system\merchant\MerchantRepository;
use think\Collection;
use think\db\BaseQuery;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;
use app\common\dao\BaseDao;
use app\common\model\BaseModel;
use think\Model;

class NewActivityProductDao extends BaseDao
{

    /**
     * @return string
     * @author: fupeng
     * @day: 2024/1/28
     */
    protected function getModel(): string
    {
        return NewActivityProduct::class;
    }

    /**
     * @return Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: fupeng
     * @day: 2024/1/28
     */
    public function getAll()
    {
        return NewActivityProduct::getDB()->select();
    }

    /**
     * @param array $where
     * @return BaseQuery
     * @author: fupeng
     * @day: 2024/1/28
     */
    public function search(array $where,?int $merId = null)
    {

        $query =NewActivityProduct::getDB()->alias('NewActivityProduct');
        $query->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
            getModelTime($query, $where['date'], 'create_time');
        });
        $query->when(($merId !== null), function ($query) use ($merId) {
            $query->where('NewActivityProduct.mer_id', $merId);
        });
        $query->when(isset($where['status']) && $where['status'] !== '',function($query)use($where){
            if($where['status'] == -1){
                $query->where('NewActivityProduct.status','in',[-1,-2]);
            }else{
                $query->where('NewActivityProduct.status',$where['status']);
            }
        });
        $query->when(isset($where['admin_status']) && $where['admin_status'] !== '',function($query)use($where){
            $query->where('NewActivityProduct.admin_status',$where['admin_status']);
        });
//        $query->when(isset($where['is_del']) && $where['is_del'] !== '', function ($query) use ($where) {
//            $query->where('NewActivityProduct.is_del',$where['is_del']);
//        });
        $query->when(isset($where['new_activity_id']) && $where['new_activity_id'] !== '', function ($query) use ($where) {
            $query->where('new_activity_id',$where['new_activity_id']);
        });
        $query->when(isset($where['product_id_in']) && is_array($where['product_id_in'] ), function ($query) use ($where) {
            $query->where('product_id','in',$where['product_id_in']);
        });
        $query->Join('StoreSpu U', 'NewActivityProduct.product_id = U.product_id')
            ->where('U.product_type', 0);
//        $query->when(isset($where['spu_status']) && $where['spu_status'] !== '',function($query)use($where){
//                $query->where('U.status',$where['spu_status']);
//            });
//
        $query->join('StoreProduct P','NewActivityProduct.product_id = P.product_id', 'left');
        $query->when(isset($where['mer_status']) && $where['mer_status'] !== '',function($query)use($where){
            $query->where('mer_status',$where['mer_status']);
        });
        $query->when(isset($where['keyword']) && $where['keyword'] !== '',function($query)use($where){
            $query->whereLike('U.store_name',"%{$where['keyword']}%");
        });
        $query->when(isset($where['mer_name']) && $where['mer_name'] !== '',function($query)use($where){
            $make = app()->make(MerchantRepository::class);
            $mer_id = $make->search(['keyword' => $where['mer_name']])->column('mer_id');
            $query->whereIn('NewActivityProduct.mer_id',$mer_id);
        });
        $query->where('NewActivityProduct.is_del',0);
        return $query;
    }


    public function switchStatus($id, $data)
    {
        return ($this->getModel())::getDB()->where($this->getPk(),$id)->update($data);
    }

    public function allChildren($new_activity_id,$ids)
    {
        return ($this->getModel())::getDB()->where('new_activity_id',$new_activity_id)
            ->whereIn('product_id',$ids)->where('status', 1)->where('is_del', 0)
            ->column('product_id');
    }

}
